#include "StdAfx.h"
#include "Config.h"
#include "stdio.h"
#include <time.h>

extern HMODULE dllModule;
char configStr[2048];
extern char ConfigPath[MAX_PATH];

ConfigMap configMap;

void
InitConfig(){
	configMap.clear();
	memset(configStr,0,2048);
	GetPrivateProfileSectionA("dnf", configStr, 2048, ConfigPath);
	
	char *curStr = configStr;
	int len = strlen(curStr);
	
	while(len){
		int pos = 0;
		for(;pos<len;pos++){
			if(curStr[pos]=='='){
				break;
			}
		}
		
		curStr[pos] = 0;
		char* key = curStr;
		char* value = curStr + pos +1;
		
		
		configMap.insert(std::pair<std::string,std::string>(key,value));
		
		curStr = curStr + len + 1;
		len = strlen(curStr);
	}
}

DWORD
GetConfig(LPCSTR name,DWORD defaultValue){
	ConfigMap::iterator iter = configMap.find(name);
	if(iter == configMap.end()){
		return defaultValue;
	}
	return strtoul(iter->second.c_str(),NULL,10);
}

void
GetConfig(LPCSTR name,LPSTR value,DWORD length){
	ConfigMap::iterator iter = configMap.find(name);
	if(iter == configMap.end()){
		return;
	}
	strcpy_s(value,length,iter->second.c_str());
}

void 
EachConfig(PnfEachConfig EachConfig){
	ConfigMap::iterator iter;
	int index = 0;
	for(iter = configMap.begin(); iter != configMap.end(); iter++){
		if(!EachConfig(iter->first.c_str(),index++)){
			break;
		}
	}
}